<?php
/* Copyright (c) 2005 Alex Lambert <alambert at quickfire dot org>
 * Rights to this code are as documented in doc/LICENSE.
 *
 * An XML-RPC testing script.
 *
 * $Id: xmlrpc_explorer.php 8403 2007-06-03 21:34:06Z jilles $
 */

/* This script requires XML-RPC for PHP 1.1
 * You can get it at http://phpxmlrpc.sf.net
 */
include('xmlrpc.inc');

/* Set this appropriately. This needs to match
 * the information in your xmlrpc{} block.
 *
 * PATH should be set to '/xmlrpc'.
 */
define('HOST', 'collinnet.us');
define('PORT', 8080);
define('PATH', '/xmlrpc');

/* Here, you can set up the methods and their arguments.
 * 
 * keys: method names;
 * value: array of strings (friendly names of parameters, in order)
 *
 * This is a simple testing script: we only support strings.
 */
$methods = array(
	'test.method' => array('string'),
	'atheme.login' => array('account', 'password'),
	'atheme.logout' => array('authcookie', 'account'),
	'atheme.command' => array('authcookie', 'account', 'source ip', 'service', 'command', 'p1', 'p2', 'p3'),	
	'atheme.account.register' => array('account', 'password', 'email'),
	'atheme.account.verify' => array('authcookie', 'account', 'operation', 'key'),
	'atheme.account.set_password' => array('authcookie', 'account', 'password'),
	'atheme.account.metadata.set' => array('authcookie', 'account', 'key', 'value'),
	'atheme.account.metadata.get' => array('account', 'key'),
	'atheme.account.metadata.delete' => array('authcookie', 'account', 'key', 'value'),
	'atheme.account.vhost' => array('authcookie', 'account', 'vhost'),
	'atheme.channel.register' => array('authcookie', 'account', 'channel'),
	'atheme.channel.metadata.set' => array('authcookie', 'account', 'channel', 'key', 'value'),
	'atheme.channel.metadata.get' => array('channel', 'key'),
	'atheme.channel.metadata.delete' => array('authcookie', 'account', 'channel', 'key', 'value'),
	'atheme.channel.topic.set' => array('authcookie', 'account', 'channel', 'topic'),
	'atheme.channel.topic.append' => array('authcookie', 'account', 'channel', 'topic'),
	'atheme.channel.access.get' => array('authcookie', 'account', 'channel'),
);

function unserialize_xml($input, $callback = null, $recurse = false)
/* bool/array unserialize_xml ( string $input [ , callback $callback ] )
 * Unserializes an XML string, returning a multi-dimensional associative array, optionally runs a callback on all non-array data
 * Returns false on all failure
 * Notes:
    * Root XML tags are stripped
    * Due to its recursive nature, unserialize_xml() will also support SimpleXMLElement objects and arrays as input
    * Uses simplexml_load_string() for XML parsing, see SimpleXML documentation for more info
 */
{
    // Get input, loading an xml string with simplexml if its the top level of recursion
    $data = ((!$recurse) && is_string($input))? simplexml_load_string($input): $input;
    // Convert SimpleXMLElements to array
    if ($data instanceof SimpleXMLElement) $data = (array) $data;
    // Recurse into arrays
    if (is_array($data)) foreach ($data as &$item) $item = unserialize_xml($item, $callback, true);
    // Run callback and return
    return (!is_array($data) && is_callable($callback))? call_user_func($callback, $data): $data;
}
		//printf('<h2>Results</h2>');
if (isset($_REQUEST['method'])){
		$method = $_REQUEST['method'];
	if (isset($method)) {
		if (array_key_exists($method, $methods))
		{
			$message = new xmlrpcmsg($method);
			foreach ($methods[$method] as $name)
				$message->addParam(new xmlrpcval($_REQUEST['in_' . $name], $xmlrpcString));
			$client = new xmlrpc_client(PATH, HOST, PORT);
			$response = $client->send($message);
		}
	}
}
				
function XML2Array ( $xml , $recursive = false )
{
    if ( ! $recursive )
    {
        $array = simplexml_load_string ( $xml ) ;
    }
    else
    {
        $array = $xml ;
    }
    
    $newArray = array () ;
    $array = ( array ) $array ;
    foreach ( $array as $key => $value )
    {
        $value = ( array ) $value ;
        if ( isset ( $value [ 0 ] ) )
        {
            $newArray [ $key ] = trim ( $value [ 0 ] ) ;
        }
        else
        {
            $newArray [ $key ] = XML2Array ( $value , true ) ;
        }
    }
    return $newArray ;
}
if(isset($response)){
$xmlunserial = unserialize_xml($response->serialize());
$xmlresult = $xmlunserial['params']['param']['value']['string'];
//var_dump($xml);
	if($response->faultCode()) {
				printf('<h3>Fault</h3>');
				printf('Fault code: %d<br>', htmlentities($response->faultCode()));
				printf('Fault string: %s', htmlentities($response->faultString()));
			}

}
?>
